package com.smart.village.big.screen.controller.bigscreen;

import com.alibaba.fastjson.JSONObject;
import com.smart.village.big.screen.domain.AjaxResult;
import com.smart.village.big.screen.domain.StudyFiles;
import com.smart.village.big.screen.service.StudyFilesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 文件类
 */
@Tag(name = "文件类数据接口")
@RestController
@RequestMapping("/study/files")
public class StudyFilesController {
    @Autowired
    private StudyFilesService studyFilesService;

    @Operation(summary = "获取大屏 文件类 根据分类获取列表")
    @Parameters({
            @Parameter(name = "category", description = "视频分类"),})
    @GetMapping("/getStudyFilesByCategory")
    @ResponseBody
    public AjaxResult getStudyFilesByCategory(@RequestParam(required = false) String category,
                                              @RequestParam(required = false) @Parameter(description = "搜索") String search,
                                              @RequestParam(required = false, defaultValue = "0") @Parameter(description = "环境标识 0 大屏 1 小程序") int environment,
                                              @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
                                              @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
            pageNum = 1;
        }
        if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        List<JSONObject> studyFilesByCategory = studyFilesService.getStudyFilesByCategory(category, search, environment, pageNum, pageSize);
        for (JSONObject studyFile : studyFilesByCategory) {
            if (studyFile != null) {

                List<StudyFiles> studyFiles = (List) studyFile.get("studyFiles");
                studyFiles.forEach(studyFiles1 -> {
                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try {
                        if (null != studyFiles1.getStudyFilesPublishDatetime() && !Objects.equals(studyFiles1.getStudyFilesPublishDatetime(), "") &&(new Date().getTime() - dateFormat.parse(studyFiles1.getStudyFilesPublishDatetime()).getTime()) / (24 * 60 * 60 * 1000) <= 3) {
                            studyFiles1.setIfNew("1");
                        } else {
                            studyFiles1.setIfNew("0");
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                        studyFiles1.setIfNew("0");
                    }

                });


            }
        }
        return AjaxResult.success(studyFilesByCategory);
    }

    @Operation(summary = "获取大屏 文件类 详情")
    @Parameters({
            @Parameter(name = "studyFileSerialNumber", description = "主键"),})
    @GetMapping("/getStudyFilesDetail")
    @ResponseBody
    public AjaxResult getStudyFilesDetail(@RequestParam String studyFileSerialNumber,@RequestParam(required = false) String openld) {
        return AjaxResult.success(studyFilesService.getStudyFilesDetail(studyFileSerialNumber,openld));
    }

    @Operation(summary = "小程序-根据 openld 获取个人书架")
    @Parameters({
            @Parameter(name = "openld", description = "openld"),})
    @GetMapping("/getPersonalBookshelf")
    @ResponseBody
    public AjaxResult getPersonalBookshelf(@RequestParam String openld) {
        return AjaxResult.success(studyFilesService.getPersonalBookshelf(openld));
    }

    @Operation(summary = "小程序-根据 openld 获取 浏览纪录列表 表")
    @Parameters({
            @Parameter(name = "openld", description = "openld"),})
    @GetMapping("/getPersonalBrowsing")
    @ResponseBody
    public AjaxResult getPersonalBrowsing(@RequestParam String openld) {
        return AjaxResult.success(studyFilesService.getPersonalBrowsing(openld));
    }

    @Operation(summary = "小程序-根据 openld，浏览记录id 获取 浏览纪录详情")
    @Parameters({
            @Parameter(name = "openld", description = "openld"),
            @Parameter(name = "browsingSerialNumber", description = "浏览记录id"),

    })
    @GetMapping("/getPersonalBrowsingDetail")
    @ResponseBody
    public AjaxResult getPersonalBrowsingDetail(
            @RequestParam String openld,
            @RequestParam String browsingSerialNumber
    ) {
        return AjaxResult.success(studyFilesService.getPersonalBrowsingDetail(openld, browsingSerialNumber));
    }

    @Operation(summary = "小程序-红色书屋")
    @Parameters({
            @Parameter(name = "tags", description = "视频标签 多个使用逗号拼接"),
    })
    @GetMapping("/getMicroRedLookBook")
    @ResponseBody
    public AjaxResult getMicroRedLookBook(@RequestParam(required = false) String tags,
                                          @RequestParam(required = false) @Parameter(description = "搜索") String search,
                                          @RequestParam(required = false, defaultValue = "0") @Parameter(description = "环境标识 0 大屏 1 小程序") int environment,
                                          @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
                                          @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
            pageNum = 1;
        }
        if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        JSONObject getMicroRedLookBook = studyFilesService.getMicroRedLookBook(tags,search, environment, pageNum, pageSize);
        return AjaxResult.success(getMicroRedLookBook);
    }

    @Operation(summary = "小程序-红色书屋-标签列表")
    @GetMapping("/getMicroRedLookBookTagsList")
    @ResponseBody
    public AjaxResult getMicroRedLookBookTagsList() {
        return AjaxResult.success(studyFilesService.getMicroRedLookBookTagsList());
    }

    @Operation(summary = "小程序-获取所有文件标签列表")
    @GetMapping("/getStudyFilesTagsList")
    @ResponseBody
    public AjaxResult getStudyFilesTagsList() {
        return AjaxResult.success(studyFilesService.getStudyFilesTagsList());
    }
}
